package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmchannelMapper;
import com.heima.wemedia.service.WmchannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class WmchannelServiceImpl extends ServiceImpl<WmchannelMapper, WmChannel> implements WmchannelService {

    @Autowired
    private WmchannelMapper wmchannelMapper;




    @Override
    public ResponseResult getChannels() {
        return ResponseResult.okResult(list());
    }

    /**
     * 频道列表查询
     * @param map
     * @return
     */
    @Override
    public ResponseResult getchannelList(Map<String, String> map) {


        IPage<WmChannel> page = new Page<WmChannel>(Integer.valueOf(map.get("page")),Integer.valueOf(map.get("size")));
        wmchannelMapper.selectPage(page,Wrappers.<WmChannel>lambdaQuery()
                .eq(map.get("name")!= "",WmChannel::getName,map.get("name")));

        PageResponseResult pageResponseResult = new PageResponseResult((int)page.getCurrent(),(int)page.getSize(),(int)page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }

    /**
     * 新增频道
     * @param map
     * @return
     */
    @Override
    public ResponseResult saveChannel(Map<String, Object> map) {
        if (map.size()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel = new WmChannel();
        wmChannel.setDescription(map.get("description").toString());
        wmChannel.setName(map.get("name").toString());
        wmChannel.setOrd((int)map.get("ord"));
        wmChannel.setStatus((boolean)map.get("status"));
        wmChannel.setCreatedTime(new Date());

        wmchannelMapper.insert(wmChannel);

        return ResponseResult.okResult("添加频道成功");
    }

    /**
     * 修改频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateChannel(ChannelDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel = wmchannelMapper.selectById(dto.getId());
        if (wmChannel.getOrd() != 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (dto.getStatus() == true){

            wmChannel.setDescription(dto.getDescription());
            wmChannel.setName(dto.getName());
            wmChannel.setOrd(dto.getOrd());
            wmChannel.setStatus(true);
            wmChannel.setCreatedTime(new Date());
            wmchannelMapper.updateById(wmChannel);
        }else{
            wmChannel.setStatus(false);
            wmchannelMapper.updateById(wmChannel);
        }


        return ResponseResult.okResult("修改频道成功");
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(int id) {
        if (id == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel wmChannel = wmchannelMapper.selectById(id);

        if (wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (wmChannel.getOrd() != 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        wmchannelMapper.deleteById(id);
        return ResponseResult.okResult("删除频道成功");
    }


}
